//
//  GatewayPermitView.m
//  YSSmartHome
//
//  Created by yanghao on 2017/4/11.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "GatewayPermitView.h"
#import "YSCommon.h"
#import "MQTTSessionManager.h"
@interface GatewayPermitView ()


@end
@implementation GatewayPermitView

- (void)dealloc
{
    debugLogInfo(@"GatewayPermitView dealloc");
}
+ (instancetype)gatewayPermitView
{
   return  [[NSBundle mainBundle]loadNibNamed:@"GatewayPermitView" owner:self options:nil].lastObject;
}
- (void)setModel:(ZTGatewayBindModel *)model
{
    _model = model;
    self.titleLable.text = model.info;
}
- (IBAction)rejectBtnTapped:(UIButton *)sender {
    if (self.delegate&& [self.delegate respondsToSelector:@selector(gatewayPermitWatch:type:)]) {
        [self.delegate gatewayPermitWatch:self type:GatewayPermitTypeReject];
    }
    
}
- (IBAction)permitWatchBtnTapped:(UIButton *)sender {
    if (self.delegate&& [self.delegate respondsToSelector:@selector(gatewayPermitWatch:type:)]) {
        [self.delegate gatewayPermitWatch:self type:GatewayPermitTypeWatch];
    }
    [self sendBindWithType:GatewayPermitTypeWatch];
    
}
- (IBAction)permitControlBtnTapped:(UIButton *)sender {
    if (self.delegate&& [self.delegate respondsToSelector:@selector(gatewayPermitWatch:type:)]) {
        [self.delegate gatewayPermitWatch:self type:GatewayPermitTypeControl];
    }
    [self sendBindWithType:GatewayPermitTypeControl];
}

- (void)sendBindWithType:(GatewayPermitType)type
{
    NSMutableArray* datas = [NSMutableArray array];
    XAITYPEAPSN applyApsn = self.model.applyApsn;
    XAITYPELUID applyLuid = self.model.applyLuid;
    XAITYPEAPSN triggerApsn = self.model.triggerApsn;
    XAITYPELUID triggerLuid = self.model.triggerLuid;
    
    XAITLV* tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_APSN bytes:&applyApsn length:sizeof(applyApsn)];
    [datas addObject:tlv];
    tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_LUID bytes:&applyLuid length:sizeof(applyLuid)];
    [datas addObject:tlv];
    tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_APSN bytes:&triggerApsn length:sizeof(triggerApsn)];
    [datas addObject:tlv];
    tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_LUID bytes:&triggerLuid length:sizeof(triggerLuid)];
    [datas addObject:tlv];
    tlv = [[XAITLV alloc]initWithType:XAI_DATA_TYPE_BIN_DIGITAL_UNSIGN bytes:&type length:sizeof(type)];
    [datas addObject:tlv];
    NSError* error =  [kMQTTSessionManager.xai sendCMD:0x03 toApsn:kZTCloudGatewayApsn toLuid:kGATEWAYLUID datas:datas];
    if (!error.code ) {
        debugLogInfo(@"绑定成功了");
    }
    else
    {
        debugLogInfo(@"%@",error.localizedDescription);
    }
}

@end
